package com.bite.book.controller;

import com.bite.book.model.UserInfo;
import com.bite.book.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.bite.book.constants.Constant.SESSION_USER_KEY;

/*
* 接口文档:
* 用户登录:
* 1. url:/user/login
* 2. 参数: String userName, String password
* 3. 返回: true 登陆成功 false 登陆失败
*

*
* */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService = new UserService();

    /**
     * 用户登录
     * url: /user/login
     * @param userName
     * @param password
     * @param session
     * @return Boolean
     */
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){
        System.out.println("接收到参数" + userName + password);
        //账号或密码为空
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            System.out.println("账号或密码为空");

            return false;
        }
        UserInfo userInfo = userService.queryUserByName(userName);
        if(userInfo == null){
            System.out.println("用户不存在");

            //用户名不存在
            return false;
        }
        if(userInfo.getUserName().equals(userName) && userInfo.getPassword().equals(password)){
            //把此用户的全部信息存储到session中
            session.setAttribute(SESSION_USER_KEY, userInfo);
            //验证通过
            return true;
        }
        System.out.println("密码错误");
//密码错误
        return false;
    }


}
